<template>
    <div class="goods-list">
        <!-- 
            <router-link :to="'/home/goodsinfo/:'+item.id" tag="div" class="goods-item" v-for="item in goodsList" :key="item.id">
            </router-link>
         -->
        <div class="goods-item" v-for="item in goodsList" :key="item.id" v-on:click="getDetail(item.id)">
            <img :src="item.img_url" alt="">
            <h3 class="title">华为全网通Note-note7 16G版</h3>
            <div class="info">
                <p class="price">
                    <span class="now">￥{{item.sell_price}}</span>
                    <span class="old">￥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩{{item.stock_quantity
}}件</span>
                </p>
            </div>
        </div>
        <mt-button type="danger" size="large" v-on:click="getMore">加载更多</mt-button>
    </div>
   
</template>
<script>
    export default {
        data() {
            return {
                goodsList: [],
                pageindex: 1
            }
        },
        created() {
            this.getGoodsList();
        },
        methods: {
            getGoodsList() {
                this.$http.get('http://vue.studyit.io/api/getgoods?pageindex='+this.pageindex).then(result=>{
                    // console.log(result.body);
                    if( result.body.status === 0 ) {
                        this.goodsList = this.goodsList.concat(result.body.message);
                    }
                });
            },
            getMore() {
                this.pageindex++;
                this.getGoodsList();
            },
            // 跳转到详情
            getDetail(id) {
                // this.$route  路由中的【参数对象】，所有的路由参数，params，query都属于它。获取url中的参数 
                // this.$router 是路由【导航对象】，使用它可以实现【前进，后退，跳转到新的url地址】。
                // 第1种用法
                // this.$router.push('/home/goodinfo/'+id);
                // 第2种用法
                // this.$router.push({ path: '/home/goodsinfo/'+id });
                this.$router.push({ name:'goodsinfo', params: { id } })
            }
        }
    }
</script>
<style lang="less" scoped>
.goods-list{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding:7px;
    .goods-item{
        width: 49%;
        border:1px solid #ccc;
        margin: 3px 0;
        padding: 2px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        min-height: 290px;
        img{
            width: 100%;

        }
        .title{
            font-size: 14px;
        }
        .info{
            background: #eee;
            p{
                margin: 0;
                padding:5px;
            }
            .price{
                .now{
                    color: #f00;
                    font-weight: bold;
                    font-size: 16px;
                }
                .old{
                    text-decoration: line-through;
                    margin-left: 10px;
                }
            }
            .sell{
                display: flex;
                justify-content: space-between;
            }
        }
    }
}
</style>
